<?php
namespace Core;
class Server
{
    public $listener;
	/**
	 * 构造方法
	 * @param string $ip 绑定IP
	 * @param int $port 监听端口
	 */
	public function __construct($ip,$port)
	{
		parent::__construct($ip, $port);
	}

    /**
     * 启动服务
     * @param $handler
     * @param array $config
     */
	public function run($handler,  array $config)
	{
		$this->set($config);
		if (defined('ORDER_IP') && defined('ORDER_IP')) {
            $this->listener= $this->addlistener(ORDER_IP,ORDER_PORT,SWOOLE_SOCK_TCP);
        }
        
        $this->on('start', array($handler, 'onStart'));
        $this->on('ManagerStart', array($handler, 'onManagerStart'));
		$this->on('WorkerStart', array($handler, 'onWorkerStart'));
        $this->on('WorkerStop', array($handler, 'onWorkerStop'));
		$this->on('connect', array($handler, 'onConnect'));
		$this->on('receive', array($handler, 'onReceive'));
		$this->on('close', array($handler, 'onClose'));
        $this->on('shutdown', array($handler, 'onShutdown'));
        $this->on('Task', array($handler, 'onTask'));
        $this->on('Finish', array($handler, 'onFinish'));

        if (defined('ORDER_IP') && defined('ORDER_IP')) {
            $this->listener->set(config('listener'));
            $this->listener->on('connect', array($handler, 'onListenerConnect'));
            $this->listener->on('receive', array($handler, 'onListenerReceive'));
            $this->listener->on('close', array($handler, 'onListenerClose'));
        }

        if (method_exists($handler, 'getProcess')) {
            if ($process = $handler->getProcess()) {
                foreach ($process as $p) {
                    print_r($p);
                    $this->addProcess($p);
                }
            }
        }

      	$this->start();
	}
}